package com.tucc.hello.leetcode.no1_400.no1_200.no1_20.no1_10.no3;

import java.util.HashMap;
import java.util.Map;

/**
 * @author tucc
 * @description todo
 * @package com.tucc.hello.leetcode
 * @date 2021/5/20
 **/
public class Solution {

    public static void main(String[] args) {
        Solution solution = new Solution();
        String s1 = "abcabcbb";
        String s2 = "bbabbbcd";
        String s3 = "abcabcbb";
        String s4 = " ";
        String s5 = "";
        System.out.println(solution.lengthOfLongestSubstring(s1));
        System.out.println(solution.lengthOfLongestSubstring(s2));
        System.out.println(solution.lengthOfLongestSubstring(s3));
        System.out.println(solution.lengthOfLongestSubstring(s4));
        System.out.println(solution.lengthOfLongestSubstring(s5));


    }

    public int lengthOfLongestSubstring(String s) {
        int max = 0;
        int curStart = 0;
        Map<Character, Integer> map = new HashMap<>();
        for(int i = 0;i < s.length();i ++){
            char ch = s.charAt(i);
            Integer preIndex = map.get(ch);
            if(preIndex != null && preIndex >= curStart){
                max = Math.max(max, i - curStart);
                curStart = preIndex + 1;
            }
            map.put(ch, i);
        }
        max = Math.max(max, s.length() - curStart);
        return max;
    }
}
